//
//  NMPOSettingViewController.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOSettingViewController: NMPOBaseCollectionViewController {
    
    // MARK: - nmpo action
    override func nmpo_layout() -> UICollectionViewFlowLayout {
        let nmpo_layout = UICollectionViewFlowLayout()
        nmpo_layout.itemSize = CGSize(width: nmpo_SCREEN_WIDTH, height: 40)
        nmpo_layout.headerReferenceSize = CGSize(width: nmpo_SCREEN_WIDTH, height: 230)
        nmpo_layout.minimumLineSpacing = 0
        return nmpo_layout
    }
    override func nmpo_collectionViews() {
        super.nmpo_collectionViews()
        view.addSubview(nmpo_navView)
        nmpo_collectionView.snp.makeConstraints { make in
            make.left.width.bottom.equalToSuperview()
            make.top.equalTo(nmpo_navView.snp.bottom)
        }
       
        nmpo_collectionView.register(NMPOSettingHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: NMPOSettingHeadView.nmpo_viewToString)
        nmpo_collectionView.dataSource = self
        nmpo_collectionView.delegate = self
        nmpo_collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: NMPOCommon.nmpo_tabbarH+15, right: 0)
        nmpo_collectionView.register(NMPOSettingCell.classForCoder(), forCellWithReuseIdentifier: NMPOSettingCell.nmpo_viewToString)
    }
    // MARK: - nmpo lazy
    override func nmpo_baseBgName() -> String? {
        return "nmpo_setting_bg"
    }
    lazy var nmpo_navView: NMPONavView = {
        let nmpo_navView = NMPONavView()
        return nmpo_navView
    }()
    lazy var nmpo_settingModels: [NMPOSettingModel] = {
        var nmpo_settingModels = [NMPOSettingModel]()
        nmpo_settingModels.append(NMPOSettingModel(nmpo_type: .nmpo_rate,nmpo_title: "Rate Us", nmpo_icon: "nmpo_setting_rate"))
        nmpo_settingModels.append(NMPOSettingModel(nmpo_type: .nmpo_share,nmpo_title: "Share", nmpo_icon: "nmpo_setting_share"))
        nmpo_settingModels.append(NMPOSettingModel(nmpo_type: .nmpo_privacy,nmpo_title: "Privacy Policy", nmpo_icon: "nmpo_setting_privacy"))
        return nmpo_settingModels
    }()
}

extension NMPOSettingViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return nmpo_settingModels.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOSettingCell.nmpo_viewToString, for: indexPath) as! NMPOSettingCell
        nmpo_cell.nmpo_settingModel = nmpo_settingModels[indexPath.row]
        return nmpo_cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let nmpo_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: NMPOSettingHeadView.nmpo_viewToString, for: indexPath) as! NMPOSettingHeadView
        return nmpo_headView
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let nmpo_settingModel = nmpo_settingModels[indexPath.row]
        if nmpo_settingModel.nmpo_type == .nmpo_rate {
            NMPOManager.nmpo_manager.nmpo_switchAppStore()
            return
        }
        if nmpo_settingModel.nmpo_type == .nmpo_share {
            NMPOManager.nmpo_manager.nmpo_shareApp(nmpo_vc: self)
            return
        }
        navigationController?.pushViewController(NMPOSettingPrivacyViewController(), animated: true)
    }
}

